package com.v1.video.activity;

import java.util.ArrayList;
import java.util.List;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.v1.video.R;
import com.v1.video.domain.VideoCategorieConfig;
import com.v1.video.domain.VideoCategorieInfo;
import com.v1.video.engine.NetEngine;
import com.v1.video.exception.ServicesException;
import com.v1.video.util.Utils;

public class SelectCatalogActivity extends BaseActivity {

	private LayoutInflater mInflater;
	private View mCancel;
	private View mConfirm;
	private GridView mGV;
	private MyAdapter mAdapter;
	private int mCurSelIndex = -1;
	private String mCurName = "";
/*	
	String[] catalogs = new String[] { "热点", "娱乐", "社会", "科技", "宝宝", "旅游",
			"创意", "萌宝", "美食", "热点", "运动", "时尚" };*/
	
	private List<VideoCategorieInfo>  catalogs = new ArrayList<VideoCategorieInfo>();
	
	@Override
	protected void onCreate(Bundle inState) {
		super.onCreate(inState);
		mInflater = this.getLayoutInflater();
		setContentView(R.layout.select_catalog_lay);
		Bundle bundle = this.getIntent().getExtras();
		if(bundle != null && bundle.containsKey("data")){
			mCurName=bundle.getString("data").split("\\|")[0];
		}
	}

	@Override
	protected void initView() {
		mCancel = findViewById(R.id.tv_cancel);
		mConfirm = findViewById(R.id.tv_confirm);
		((TextView) findViewById(R.id.tv_caption)).setText(this.getResources()
				.getString(R.string.titlebar_title_addcatalog));
		findViewById(R.id.iv_back).setVisibility(View.GONE);
		findViewById(R.id.tv_title);
		mCancel.setVisibility(View.VISIBLE);
		mConfirm.setVisibility(View.VISIBLE);
		
		mGV = (GridView) findViewById(R.id.gridView1);

	}

	@Override
	protected void initData() {
		mAdapter = new MyAdapter();
		mGV.setAdapter(mAdapter);
	    new GetDataTask(this).execute();
	}

	@Override
	protected void setListener() {
		mCancel.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				back();
			}});
		
		mConfirm.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				if(mCurSelIndex!=-1){
					Intent intent=new Intent();
					intent.putExtra("data", catalogs.get(mCurSelIndex).getName()+"|"+catalogs.get(mCurSelIndex).getId());
					setResult(RESULT_OK,intent);
					finish();
				}
			}});

	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			back();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}
	
	
	private class GetDataTask extends AsyncTask<Void, Void, Void> {
		private String errorMsg = "";
		private ProgressDialog pd;
		VideoCategorieConfig videoCates;
		private Context mCxt;

		public GetDataTask(Context cxt) {
			mCxt = cxt;
		}

		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			pd = Utils.getProgressDialog(mCxt, getResources()
					.getString(R.string.get_data), this);
			pd.show();
		}

		@Override
		protected Void doInBackground(Void... params) {
			try {
				NetEngine engine = new NetEngine();
				videoCates = engine.getVideoCategorieInfo();
			} catch (ServicesException e) {
				errorMsg = e.getMessage();
				e.printStackTrace();
			} 
			return null;
		}

		@Override
		protected void onPostExecute(Void result) {
			super.onPostExecute(result);
			if (pd != null && pd.isShowing()) {
				pd.cancel();
			}
			if (errorMsg.equals("")) {
				if (videoCates != null) {
					if (videoCates.getObj().size() > 0) {
						catalogs.clear();
						catalogs.addAll(videoCates.getObj());
						mAdapter.notifyDataSetChanged();
					}
				}
			} else {
				Toast.makeText(mCxt, errorMsg,
						Toast.LENGTH_SHORT).show();
			}
		}
	}

	private void back() {
		setResult(RESULT_CANCELED, new Intent());
		finish();
	}
	
	private class MyAdapter extends BaseAdapter{

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return catalogs.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return catalogs.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public View getView(final int position, View convertView, ViewGroup parent) {
			PlaceHolder holder = null;
			if(convertView == null){
				convertView = mInflater.inflate(R.layout.select_catalog_lay_item, null);
				holder = new PlaceHolder();
				holder.txt = (TextView) convertView.findViewById(R.id.textView1);
				holder.imageView1 = (ImageView)convertView.findViewById(R.id.imageView1);
				convertView.setTag(holder);
			}else{
				holder = (PlaceHolder) convertView.getTag();
			}
			
			holder.txt.setText(catalogs.get(position).getName());
			if(mCurSelIndex == -1 && mCurName.equals(catalogs.get(position).getName())){
				mCurSelIndex = position;
			}
			if(position == mCurSelIndex){
				holder.txt.setBackgroundResource(R.drawable.shape_topic_pressed);
				holder.imageView1.setVisibility(View.VISIBLE);
			}else{
				holder.txt.setBackgroundResource(R.drawable.select_topic_selector);
				holder.imageView1.setVisibility(View.GONE);
			}
			holder.txt.setOnClickListener(new OnClickListener(){

				@Override
				public void onClick(View v) {
					mCurSelIndex = position;
					MyAdapter.this.notifyDataSetChanged();
				}});
			
			return convertView;
		}
		
		private class PlaceHolder{
			TextView txt;
			ImageView imageView1;
		}
		
	}

}
